﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace SharePoint.Orm.Caml {
	public class CamlOrderBy {
		public List<CamlFieldRef> Fields { get; set; }

		public CamlOrderBy() {
			Fields = new List<CamlFieldRef>();
		}

		public XElement ToXml() {
			XElement orderByElement = new XElement("OrderBy");
			foreach (CamlFieldRef fieldRef in Fields) {
				orderByElement.Add(fieldRef.ToXml());
			}
			return orderByElement;
		}

		public static CamlOrderBy Parse(XElement element) {
			CamlOrderBy result = new CamlOrderBy();
			foreach (XElement child in element.Elements()) {
				if (child.Name == "FieldRef") {
					result.Fields.Add(CamlFieldRef.Parse(child));
				} else {
					throw new Exception("Unexpected element '" + child.Name + "'");
				}
			}
			return result;
		}
	}
}
